package cn.wolfcode.wolf2w.web.controller;

import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.StrategyTheme;
import cn.wolfcode.wolf2w.query.StrategyQuery;
import cn.wolfcode.wolf2w.service.IStrategyCatalogService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.IStrategyThemeService;
import cn.wolfcode.wolf2w.uitl.JsonResult;
import cn.wolfcode.wolf2w.vo.CatalogVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("strategy")
public class StrategyController {
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IStrategyCatalogService strategyCatalogService;
    @Autowired
    private IStrategyThemeService strategyThemeService;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") StrategyQuery qo){
       Page<Strategy> page =strategyService.query(qo);
       model.addAttribute("page",page);
        //直接返回页面
        return "strategy/list";
    }
    
    /**
     * 通过区域id删除数据
     * @par
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(String id){
        strategyService.delete(id);
        return JsonResult.success();
    }
    @RequestMapping("/input")
    public String input(Model model,String id){
        if (StringUtils.hasLength(id)){
            //通过id查询当前对象
            Strategy strategy = strategyService.get(id);
            model.addAttribute("strategy",strategy);
        }
        //catalogs 主题分类,并排序
        List<CatalogVo> catalogs = strategyCatalogService.catalogLists();
        //themes主题并排序
        List<StrategyTheme> strategyThemes = strategyThemeService.findByOrderBySequence();
        model.addAttribute("catalogs",catalogs);
        model.addAttribute("themes",strategyThemes);
        return "strategy/input";
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOUpdate(Strategy strategy){
       /* //NoNullStringUitl.getNull(strategy.getSequence(),"");
        //直接返回页面
        if (StringUtils.hasLength(strategy.getId())){
            //1先查询  2再替换  3修改
            //这个是特殊的类,
            strategyService.update(strategy);
        }else {
          strategyService.save(strategy);
        }*/
        strategyService.saveOUpdate(strategy);
       return JsonResult.success();
    }
    /**
     * 修改部分属性
     * @par
     * @return
     */
    @RequestMapping("/changeState")
    @ResponseBody
    public Object changeState(int state,String id){
        strategyService.changeState(id,state);
        return JsonResult.success();
    }
}
